package io.renren.modules.mapp.controller;

import com.alibaba.fastjson.JSONObject;
import io.renren.common.utils.R;
import io.renren.modules.api.annotation.AuthIgnore;
import io.renren.modules.mapp.Constants;
import io.renren.modules.mapp.service.ActivityService;
import io.renren.modules.mapp.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;


/**
 * Created by Administrator on 2017/7/31 0031.
 */
@RestController
@RequestMapping("/mapp")
public class LoginController {
    @Autowired
    private ActivityService activityService;
    /**
     * 登录
     */
    @Autowired
    private LoginService loginService;

    @AuthIgnore
    @PostMapping("login")
    public R login(@RequestHeader(Constants.WX_HEADER_CODE) String code, @RequestHeader(Constants.WX_HEADER_ENCRYPTED_DATA) String encryptedData,
                   @RequestHeader(Constants.WX_HEADER_IV) String iv) {

        JSONObject res = null;
        R r = new R();
        res = loginService.login(code, encryptedData, iv);
        Map mapRes = res.toJavaObject(Map.class);
        r.putAll(mapRes);
        return r;
    }

    @PostMapping("wxlogin")
    public R login() {
        R r = new R();
        List myActi=this.activityService.queryMyActivities("1");
        List actiJoined=this.activityService.queryActivitiesJoined("");
        r.put("myActi",myActi);
        r.put("actiJoined",actiJoined);
        return r;
    }
}
